package com.unlcn.ils.kas.api.controller;

import com.google.common.collect.Lists;
import com.unlcn.ils.kas.api.vo.KasCustomerVO;
import com.unlcn.ils.kas.api.vo.ResultDTO;
import com.unlcn.ils.kas.api.vo.SAPJieSuanVO;
import com.unlcn.ils.kas.dao.model.order.bo.OrderForCustcomerBo;
import com.unlcn.ils.kas.service.order.OrderOtdService;
import com.unlcn.ils.kas.service.util.BaseException;
import com.unlcn.ils.kas.service.util.enums.order.CustomCodeEnum;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Api(value = "客户订单")
@Controller
@RequestMapping("/otd")
public class OTDController {
    private static final Logger logger = LoggerFactory.getLogger(OTDController.class);

    @Autowired
    private OrderOtdService orderOtdService;

    /**
     * 查询订单列表
     *
     * @param request
     * @param conditions
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/tranportInfo", method = RequestMethod.POST)
    public @ResponseBody @ApiOperation(value = "第三方物流的整车运输信息查询接口", httpMethod = "POST", notes = "query tranport information", response = ResultDTO.class) ResultDTO<List<KasCustomerVO>> listByConditionsForCust(
            HttpServletRequest request,
            @ApiParam(required = true, value = "查询参数, customer_order_code多个以(,或;)分隔" + "\n"
                    + "{\"customer_order_code\":\"\"," + "\n" + "\"shipment_timeF\":\"\"," + "\n"
                    + "\"shipment_timeT\":\"\"," + "\n" + "\"client_code\":\"\"," + "\n" + "\"arrive_timeF\":\"\","
                    + "\n" + "\"arrive_timeT\":\"\"," + "\n"
                    + "\"order_code\":\"\"}", name = "conditions") @RequestBody Map<String, Object> conditions)
            throws Exception {
        logger.info("OTDController.listByConditionsForCust params:{}", conditions);
        ResultDTO<List<KasCustomerVO>> result = new ResultDTO<>(true, null, "1", "查询成功");
        try {
            List<OrderForCustcomerBo> listBo = orderOtdService.selectListByConditionsForCust(request, conditions);
            List<KasCustomerVO> listVo = Lists.newArrayList();
            listBo.forEach(orderForCustcomerBo -> {
                KasCustomerVO kasCustomerVO = new KasCustomerVO();
                BeanUtils.copyProperties(orderForCustcomerBo, kasCustomerVO);
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                if (!Objects.isNull(orderForCustcomerBo.getShipmentTime())) {
                    kasCustomerVO.setShipmentTime(sdf.format(orderForCustcomerBo.getShipmentTime()));
                }
                if (!Objects.isNull(orderForCustcomerBo.getArriveTime())) {
                    kasCustomerVO.setArriveTime(sdf.format(orderForCustcomerBo.getArriveTime()));
                }
                if (!Objects.isNull(orderForCustcomerBo.getInBoundUnlcn())) {
                    kasCustomerVO.setInBoundUnlcn(sdf.format(orderForCustcomerBo.getInBoundUnlcn()));
                }
                if (!Objects.isNull(orderForCustcomerBo.getOutBoundUnlcn())) {
                    kasCustomerVO.setOutBoundUnlcn(sdf.format(orderForCustcomerBo.getOutBoundUnlcn()));
                }
                listVo.add(kasCustomerVO);
            });

            if (CollectionUtils.isEmpty(listVo)) {
                result.setMessageCode("-1");
                result.setMessage("查询结果为空");
                result.setSuccess(true);
            } else {
                result.setData(listVo);
            }
        } catch (BeansException e) {
            logger.error("OTDController.listByConditionsForCust error :{}.", e);
            result.setMessageCode("-1");
            throw new BaseException("查询运单接口出错");
        }
        return result;
    }

    /**
     * 反馈结算参数给JMC的SAP
     *
     * @param request
     * @param conditions
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/jmcInfo", method = RequestMethod.POST)
    public @ResponseBody @ApiOperation(value = "反馈结算参数给JMC的SAP", httpMethod = "POST", notes = "反馈结算参数给JMC的SAP", response = ResultDTO.class) ResultDTO<List<SAPJieSuanVO>> listByConditionsForJMC(
            HttpServletRequest request,
            @ApiParam(required = true, value = "查询参数,order_code多个以(,或;)分隔 " + "\n\n" + "{\"refresh_timeF\":\"\"," + "\n"
                    + "\"refresh_timeT\":\"\"," + "\n"
                    + "\"order_code\":\"\"}", name = "conditions") @RequestBody Map<String, Object> conditions)
            throws Exception {
        logger.info("OTDController.listByConditionsForJMC params:{}", conditions);
        ResultDTO<List<SAPJieSuanVO>> result = new ResultDTO<>(true, null, "1", "查询成功");
        try {
            if (null != conditions) {
                conditions.put("client_code", CustomCodeEnum.JMC.getCode());
            }
            List<OrderForCustcomerBo> listBo = orderOtdService.selectListByConditionsForCust(request, conditions);
            List<SAPJieSuanVO> listVo = Lists.newArrayList();
            listBo.forEach(orderForCustcomerBo -> {
                SAPJieSuanVO sapJieSuanVO = new SAPJieSuanVO();
                BeanUtils.copyProperties(orderForCustcomerBo, sapJieSuanVO);
                SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
                if (!Objects.isNull(orderForCustcomerBo.getArrangeTime())) {
                    sapJieSuanVO.setArrangeTime(sdf.format(orderForCustcomerBo.getArrangeTime()));
                }
                if (!Objects.isNull(orderForCustcomerBo.getArriveTime())) {
                    sapJieSuanVO.setArriveTime(sdf.format(orderForCustcomerBo.getArriveTime()));
                }
                if (!Objects.isNull(orderForCustcomerBo.getInBoundUnlcn())) {
                    sapJieSuanVO.setInBoundUnlcn(sdf.format(orderForCustcomerBo.getInBoundUnlcn()));
                }
                if (!Objects.isNull(orderForCustcomerBo.getOutBoundUnlcn())) {
                    sapJieSuanVO.setOutBoundUnlcn(sdf.format(orderForCustcomerBo.getOutBoundUnlcn()));
                }
                if (!Objects.isNull(orderForCustcomerBo.getReturnOrderTime())) {
                    sapJieSuanVO.setReturnOrderTime(sdf.format(orderForCustcomerBo.getReturnOrderTime()));
                }
                if (!Objects.isNull(orderForCustcomerBo.getRefreshTime())) {
                    sapJieSuanVO.setRefreshTime(sdf.format(orderForCustcomerBo.getRefreshTime()));
                }
                listVo.add(sapJieSuanVO);
            });

            if (CollectionUtils.isEmpty(listVo)) {
                result.setMessageCode("-1");
                result.setMessage("查询结果为空");
                result.setSuccess(true);
            } else {
                result.setData(listVo);
            }
        } catch (BeansException e) {
            logger.error("OTDController.listByConditionsForJMC error :{}.", e);
            result.setMessageCode("-1");
            throw new BaseException("查询JMC结算参数接口出错");
        }
        return result;
    }
}
